package acs.bridge.jreport;

import acs.bridge.BridgeModuleException;
import acs.bridge.utility.UtilityParameters;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;


/**
 * acs.jreport
 * User: KuznetsovStV
 * Date: 04.02.12
 * Time: 13:51
 * Group of reports
 */
public class JReportCollection {
    Logger logger = Logger.getLogger(this.getClass().getName());

    private Collection<JReportInterface> reportCollection;

    private Collection<JReportItem> ReportItemCollection;
    private String reportFileName;
    private String postfix;
    private Properties parameters;
    private String dbCommandLine;
    private JdbcTemplate jdbcTemplate;


    public void setReportSaveCollection(Collection<JReportSaveInterface> reportSaveCollection) {
        this.reportSaveCollection = reportSaveCollection;
    }

    private Collection<JReportSaveInterface> reportSaveCollection;


    public void setReportCollection(Collection<JReportInterface> reportCollection) {
        this.reportCollection = reportCollection;
    }

    public Properties getParameters() {
        return parameters;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void makeReportCollection() throws BridgeModuleException {
        logger.info("makeReportCollection");
        Iterator iterator = reportCollection.iterator();
        JReportInterface jReportInterface = null;
        ReportItemCollection = new ArrayList<JReportItem>();
        try {
            logger.info("getConnection");
            if (jdbcTemplate.getDataSource().getConnection() == null) {
                logger.error("Connection not found");
            }
            logger.info("getConnection completed");
            if (dbCommandLine != null && jdbcTemplate != null) {
                logger.info("combineProperties");
                parameters = UtilityParameters.combineProperties(parameters, UtilityParameters.getParamRaw(jdbcTemplate.getDataSource().getConnection(), dbCommandLine));
                logger.info("combineProperties completed");
            }

            if (parameters != null) {
                Iterator it = parameters.keySet().iterator();

                while (it.hasNext()) {
                    String key = (String) it.next();
                    if (key.equals("%reportFileName%")) {
                        if (reportFileName == null)
                            reportFileName = parameters.getProperty(key);
                        else
                            reportFileName = reportFileName.concat(parameters.getProperty(key));
                    }
                }
            }
            while (iterator.hasNext()) {
                jReportInterface = (JReportInterface) iterator.next();
                jReportInterface.addParameters(parameters);
                jReportInterface.setDbCommandLineExt(dbCommandLine);
                logger.info("makeReport");
                JReportItem jReportItem = jReportInterface.makeReport();
                logger.info("makeReport completed");
                if (jReportItem != null)
                    ReportItemCollection.add(jReportItem);
            }


            Iterator iteratorSave = reportSaveCollection.iterator();
            JReportSaveInterface jReportSaveInterface = null;
            //String l_postfix = jReportItem.getReportPostfix();
            if (ReportItemCollection.size() > 0) {
                logger.info("Saving report ...");
                while (iteratorSave.hasNext()) {
                    jReportSaveInterface = (JReportSaveInterface) iteratorSave.next();
                    // if (jReportSaveInterface.getReportName() == null)
                    jReportSaveInterface.setReportName(reportFileName);
                    jReportSaveInterface.setPostfix(postfix);
                    jReportSaveInterface.setParameters(parameters);
                    //   jReportSaveInterface.setPostfix(l_postfix);
                    //  jReportSaveInterface.saveReport(jReportItem.getReportData(), jReportItem.getReportExt());
                    jReportSaveInterface.saveReportItem(ReportItemCollection);
                }
            } else
                logger.info("There aren't reports to save");

            logger.info("The work is completed successfully");

        } catch (Exception e) {
            // logger.error(e.getMessage());
            logger.error(e.getMessage());
            logger.info("The work is completed with error");
            throw new BridgeModuleException("JReportCollection error.", e);

        } finally {

        }


    }

    public void setReportFileName(String reportFileName) {
        this.reportFileName = reportFileName;
    }

    public String getReportFileName() {
        return reportFileName;
    }

    public void setPostfix(String postfix) {
        this.postfix = postfix;
    }

    public void setParameters(Properties parameters) {
        this.parameters = parameters;
    }

    public void setDbCommandLine(String dbCommandLine) {
        this.dbCommandLine = dbCommandLine;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
